package cn.byapps.attendance.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import cn.byapps.attendance.dao.FavJobDao;
import cn.byapps.attendance.entity.FavJob;
import cn.byapps.attendance.entity.Job;
import cn.byapps.attendance.service.FavJobService;
import cn.byapps.attendance.util.DataTablesResult;
import core.service.impl.BaseServiceImpl;

@Service
public class FavJobServiceImpl extends BaseServiceImpl<FavJob> implements FavJobService {

	@Resource
	private FavJobDao favJobDao;

	@Override
	public DataTablesResult<FavJob> getData(Integer draw, Integer pos, Integer rows) {
		DataTablesResult<FavJob> ads = new DataTablesResult<FavJob>();
		ads.setDraw(draw);
		ads.setData(favJobDao.find(pos, rows));
		long total = favJobDao.count();
		ads.setRecordsFiltered(total);
		ads.setRecordsTotal(total);
		return ads;
	}

	@Override
	public Map<String, Object> getEcharts(Integer records) {
		return getEcharts(null, null, records);
	}

	@Override
	public Map<String, Object> getEcharts(String dept, String clazz, int records) {
		List objs = null;
		Map<String, Object> maps = new HashMap<String, Object>();
		if (clazz != null && !clazz.equals("")) {
			objs = favJobDao.getGroupByClazzList(clazz, records);
			maps.put("title", clazz);
		} else if (dept != null && !dept.equals("")) {
			objs = favJobDao.getGroupByDeptList(dept, records);
			maps.put("title", dept);
		} else {
			objs = favJobDao.getGroupByList(records);
			maps.put("title", "");
		}
		if (objs == null && objs.size() == 0) {

		} else {
			String[] category = new String[objs.size()];
			Long[] data = new Long[objs.size()];
			for (int i = 0; i < objs.size(); i++) {
				Object[] ob = (Object[]) objs.get(i);
				category[i] = ((Job) ob[0]).getName();
				data[i] = (Long) ob[1];
			}
			maps.put("category", category);
			maps.put("data", data);
		}
		return maps;
	}

}
